package com.ww.springboot.boot.algorithm.stack;

import lombok.SneakyThrows;

import java.awt.*;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.TimeUnit;

/**
 * 描述：
 *
 * @author wanwei
 * @since 2021-06-12 19:06
 */
public class StackSelf<E> extends Stack<E> {

    private List<E> collection = new ArrayList<>();

    private String info = "";

    private static int sleep = 100;

    private static final Integer widht = 30;
    private static final Integer height = 20;


    @SneakyThrows
    @Override
    public E push(E item) {
        info = item + "入栈";
        TimeUnit.MILLISECONDS.sleep(sleep);
        collection.add(item);
        return super.push(item);
    }

    @SneakyThrows
    @Override
    public synchronized E pop() {
        info = collection.get(collection.size() - 1) + "出栈";
        TimeUnit.MILLISECONDS.sleep(sleep);
        collection.remove(collection.size() - 1);
        return super.pop();
    }

    @Override
    public synchronized E peek() {
        return super.peek();
    }

    public List<E> getCollection() {
        return collection;
    }

    public StackSelf<E> setCollection(List<E> collection) {
        this.collection = collection;
        return this;
    }

    public String getInfo() {
        return info;
    }

    public StackSelf<E> setInfo(String info) {
        this.info = info;
        return this;
    }

    public void paint(Graphics g) {
        paint(g,0);
    }

    public void paint(Graphics g, int x) {
        Font fontDefault = g.getFont();
        Color colorDefault = g.getColor();
        //画栈值
        g.setColor(Color.black);
        //画栈结构
        for (int i = 0; i < 10; i++) {
            g.drawRect(650 + x, 400 - height * i, widht, height);
            g.drawString("栈图", 650 + x, 450);
        }
        //画栈值
        g.setColor(Color.blue);
        for (int i = 0; i < collection.size(); i++) {
            g.drawString(String.valueOf(collection.get(i)), 650 + 10 + x, 430 - height * i - 10);
        }
        //提示1
        g.setColor(Color.red);
        g.setFont(new Font(null, Font.BOLD, 20));
        g.drawString(info, 700 + x, 300);

        g.setFont(fontDefault);
        g.setColor(colorDefault);
    }
}
